﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GPMS.Model;
using System.Threading;
namespace GPMS.DbTest
{
    class Program
    {
        static GPMSDbContainer gpms = new GPMSDbContainer();
        static void Main(string[] args)
        {
            TestData td = new TestData();


            td.AddSpecialty();
            td.AddUserInfo();
            td.AddMessage();
            td.AddGraduationProject();
            td.AddGPSelection();
            //td.AddProjectPlan();
            td.AddRole();
            //td.AddControllerInfo();
            //td.AddActionInfo();

            GetSelection();
            GetMessage();
            GetProjectPlan();
            GetRole();
            GetPremission();


            //MvcLinks mvc = new MvcLinks();
            //mvc.Showlinks();

            Console.WriteLine("\n按任意键结束...");
            Console.ReadKey();
        }

        static void GetMessage()
        {
            var context = (from c in gpms.Messages
                           where c.Receiver.UserName == "BB"
                           select new
                           {
                               Message = c,
                               Receiver = c.Receiver,
                               Sender = c.Sender
                           }).First();
            Console.WriteLine("Sender:{0}\nReceiver:{1}\nMessage:{2}",
                context.Sender.UserName,
                context.Receiver.UserName,
                context.Message.Context);
        }

        static void GetSelection()
        {
            var selection = from s in gpms.GPSelections
                            select new
                            {
                                Selector = s.User,
                                Guider = s.GraduationProject.Guider,
                                Project = s.GraduationProject
                            };
            selection.ToList().ForEach(s =>
                {
                    Console.WriteLine("Guilder:{0}",s.Guider.UserName);
                    Console.WriteLine("Selector:{0}", s.Selector.UserName);
                    Console.WriteLine("Project:{0}", s.Project.GPName);
                });
        }

        static void GetProjectPlan()
        {
            var plans = from p in gpms.ProjectPlans
                        where p.GPSelection.GraduationProject.GPName == "超级精英系统"
                        orderby p.BeginDate
                        select p;
            plans.ToList().ForEach(p =>
                {
                    Console.WriteLine("Description:{0}", p.Description);
                    Console.WriteLine("Begin:{0}", p.BeginDate.ToShortDateString());
                    Console.WriteLine("Finish:{0}", p.FinishDate.ToShortDateString());
                });
        }

        static void GetRole()
        {
            var roles = from r in gpms.Roles
                     where r.Users.Any(u => u.UserName == "BB")
                     select r;
            foreach (var role in roles)
            {
                Console.WriteLine(role.RoleName);
            }
        }

        static void GetPremission()
        {
            var actions = from a in gpms.ActionInfos
                          where a.Roles.Any(r => r.Users.Any(u => u.UserName == "BB"))
                          select new
                          {
                              ActionInfo = a,
                              ControllerInfo = a.ControllerInfo
                          };
            foreach (var a in actions)
            {
                Console.WriteLine("{0} - {1}",a.ControllerInfo.ControllerName,a.ActionInfo.ActionName);
            }
        }
    }
}
